package com.example.xuecheng.contentservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xuecheng.contentservice.XuechengContentServiceApplication;
import com.example.xuecheng.contentservice.mapper.*;
import com.example.xuecheng.contentservice.service.CourseBaseInfoService;
import dto.AddCourseBaseDto;
import dto.CourseBaseInfoDto;
import dto.EditCourseBaseDto;
import dto.QueryCourseParamsDto;
import exception.XueChengException;
import model.PageParams;
import model.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import po.*;

import javax.annotation.Resource;
import javax.transaction.xa.XAException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/** 课程基本信息管理业务接口实现类
 * @author xiehaidong
 */
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    @Resource
    private TeachplanMapper teachplanMapper;
    @Resource
    private CourseTeacherMapper courseTeacherMapper;
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDto queryCourseParamsDto) {
        LambdaQueryWrapper<CourseBase> courseBaseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseBaseLambdaQueryWrapper.like(StringUtils.isNotBlank(queryCourseParamsDto.getCourseName()),CourseBase::getName,queryCourseParamsDto.getCourseName());
        courseBaseLambdaQueryWrapper.eq(StringUtils.isNotBlank(queryCourseParamsDto.getAuditStatus()),CourseBase::getAuditStatus,queryCourseParamsDto.getAuditStatus());
        courseBaseLambdaQueryWrapper.eq(StringUtils.isNotBlank(queryCourseParamsDto.getPublishStatus()),CourseBase::getStatus,queryCourseParamsDto.getPublishStatus());
//分页对象
        Page<CourseBase> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        //查询结果
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, courseBaseLambdaQueryWrapper);
       return new PageResult<>(pageResult.getRecords(),pageResult.getTotal(),pageParams.getPageNo(),pageParams.getPageSize());
    }

    @Override
    public CourseBaseInfoDto addCourseBaseInfo(Long companyId, AddCourseBaseDto addCourseBaseDto) {
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(addCourseBaseDto,courseBase);
        courseBase.setCompanyId(companyId);
        //设置审核状态
        courseBase.setAuditStatus("202002");
//        courseBase.setCreateDate(LocalDateTime.now());
        int insert=courseBaseMapper.insert(courseBase);
        if (insert<1) {
            throw  new XueChengException(500,"新增课程基本信息失败");
        }
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        CourseMarket courseMarket = new CourseMarket();
        courseMarket.setCourseId(courseBase.getId());
        BeanUtils.copyProperties(addCourseBaseDto,courseMarket);
        //收费课程
        if(Objects.equals(addCourseBaseDto.getCharge(), "201001") &&addCourseBaseDto.getPrice()<=0){
            throw  new XueChengException(500,"付费课程价格不可为空");
        }
        //免费课程
        if(Objects.equals(addCourseBaseDto.getCharge(), "201000") &&addCourseBaseDto.getPrice()!=0){
            throw  new XueChengException(500,"免费课程不可存在价格");
        }
        int result=courseMarketMapper.insert(courseMarket);
        if (result<1) {
            throw  new XueChengException(500,"新增课程营销信息失败");
        }
        return getCourseBaseInfoDto(courseBase.getId());
    }

    @Override
    public CourseBaseInfoDto editCourseBaseInfo(Long companyId, EditCourseBaseDto editCourseBaseDto) {
        Long courseId = editCourseBaseDto.getId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase==null) {
            throw new XueChengException(500,"课程基本信息不存在");
        }
        if (!courseBase.getCompanyId().equals(companyId)){
            throw new XueChengException(500,"只能由本机构成员编辑机构内课程");
        }
        BeanUtils.copyProperties(editCourseBaseDto,courseBase);
//        courseBase.setChangeDate(LocalDateTime.now());
        int update = courseBaseMapper.updateById(courseBase);
        if(update<1){
            throw new XueChengException(500,"更新课程基本信息失败");
        }
        LambdaQueryWrapper<CourseMarket> courseMarketLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseMarketLambdaQueryWrapper.eq(CourseMarket::getCourseId,courseId);
        CourseMarket courseMarket = courseMarketMapper.selectOne(courseMarketLambdaQueryWrapper);
        if (courseMarket==null) {
            throw new XueChengException(500,"课程营销信息不存在");
        }
        BeanUtils.copyProperties(editCourseBaseDto,courseMarket,new String[]{"id"});
        int update1 = courseMarketMapper.updateById(courseMarket);
        if(update1<1){
            throw new XueChengException(500,"更新课程营销信息失败");
        }
        return getCourseBaseInfoDto(courseId);
    }

    @Override
    @Transactional
    public void deleteCourse(Long companyId, Long courseId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase==null) {
            throw new XueChengException(500,"课程基本信息不存在");
        }
        if (!courseBase.getCompanyId().equals(companyId)){
            throw new XueChengException(500,"只能由本机构成员编辑机构内课程");
        }
        int delete = courseBaseMapper.deleteById(courseBase);
        if(delete<1){
            throw new XueChengException(500,"删除课程基本信息失败");
        }

    }

    public CourseBaseInfoDto getCourseBaseInfoDto(Long courseId){
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        LambdaQueryWrapper<CourseMarket> courseMarketLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseMarketLambdaQueryWrapper.eq(CourseMarket::getCourseId,courseId);
        LambdaQueryWrapper<CourseCategory> courseCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseCategoryLambdaQueryWrapper.eq(CourseCategory::getPath,courseBase.getMt());
        CourseCategory courseCategory = courseCategoryMapper.selectOne(courseCategoryLambdaQueryWrapper);
        courseBaseInfoDto.setMtName(courseCategory.getName());
        LambdaQueryWrapper<CourseCategory> courseCategoryLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        courseCategoryLambdaQueryWrapper1.eq(CourseCategory::getPath,courseBase.getSt());
        courseCategory = courseCategoryMapper.selectOne(courseCategoryLambdaQueryWrapper1);
        courseBaseInfoDto.setStName(courseCategory.getName());
        return courseBaseInfoDto;
    }

    /**
     * 删除课程营销信息
     * @param courseId
     */
    private void deleteCourseMarket(Long courseId){
        LambdaQueryWrapper<CourseMarket> courseMarketLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseMarketLambdaQueryWrapper.eq(CourseMarket::getCourseId,courseId);
        CourseMarket courseMarket = courseMarketMapper.selectOne(courseMarketLambdaQueryWrapper);
        if(courseMarket==null){
            throw new XueChengException(500,"课程营销信息不存在");
        }
        int delete=courseMarketMapper.deleteById(courseMarket);
        if(delete<1){
            throw new XueChengException(500,"删除课程营营销信息失败");
        }
        deleteCourseMarket(courseId);
        deleteCourseTeachPlan(courseId);
        deleteCourseTeacher(courseId);
    }
    private void deleteCourseTeachPlan(Long courseId){
        LambdaQueryWrapper<Teachplan> teachplanLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teachplanLambdaQueryWrapper.eq(Teachplan::getCourseId,courseId);
        List<Teachplan> teachplans = teachplanMapper.selectList(teachplanLambdaQueryWrapper);
        if(teachplans.size()==0){
          return;
        }
  teachplanMapper.delete(teachplanLambdaQueryWrapper);
    }
    private void deleteCourseTeacher(Long courseId){
        LambdaQueryWrapper<CourseTeacher> courseTeacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseTeacherLambdaQueryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(courseTeacherLambdaQueryWrapper);
        if(courseTeachers.size()==0){
            return;
        }
       courseTeacherMapper.delete(courseTeacherLambdaQueryWrapper);
    }


}
